package info.scholtus.open.ui;

import static info.scholtus.open.util.LogUtils.LOGI;
import static info.scholtus.open.util.LogUtils.makeLogTag;
import info.scholtus.open.R;
import info.scholtus.open.provider.Contract.Categories;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockActivity;

public class CategoryAddActivity extends SherlockActivity {
	
	private String TAG = makeLogTag(CategoryAddActivity.class);
	
	private EditText mName;
	private EditText mAbstract;
	private Uri mUri;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mUri = getIntent().getData();
		
		LOGI(TAG,mUri.toString());
		
		setContentView(R.layout.activity_category_add);
		setTitle(R.string.menu_category_add);
		
		mName = (EditText) findViewById(R.id.edittext_name);
		mAbstract = (EditText) findViewById(R.id.edittext_abstract);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		
		
		if (mName.getText().toString().equals("")) {
			if (!mUri.equals(Categories.CONTENT_URI)) {
				getContentResolver().delete(mUri, null, null);
			}
		}
		else {
			ContentValues values = new ContentValues();
			values.put(Categories.CATEGORY_NAME,mName.getText().toString());
			values.put(Categories.CATEGORY_ABSTRACT,mAbstract.getText().toString());
			
			if (mUri.equals(Categories.CONTENT_URI)) {
				mUri = getContentResolver().insert(mUri, values);
				setResult(RESULT_OK, new Intent().setData(mUri));
			}
			else {
				getContentResolver().update(mUri, values, null, null);
			}
		}
	}
}
